<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('业务时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('开始时间')"
                class="query-group-cust"
                v-model="queryParam.busiTimeBeg"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('结束时间')"
                class="query-group-cust"
                v-model="queryParam.busiTimeEnd"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('会员信息')">
              <a-input :placeholder="$t('请输入手机号/卡号/姓名')" v-model="queryParam.memberInfo"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('业务员信息')">
                <a-input :placeholder="$t('请输入手机号/姓名')" v-model="queryParam.salesmanInfo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('业务类型')">
                <j-dict-select-tag
                  :placeholder="$t('请选择业务类型')"
                  v-model="queryParam.busiType"
                  dictCode="bis_order.type"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('支付方式')">
                <a-select v-model="queryParam.payType">
                  <a-select-option :value="item.name" :key="index" v-for="(item, index) in paytypeList">{{
                    item.nameText
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
              <a-button
                type="primary"
                icon="download" :loading="exloading"
                style="margin-left: 8px"
                @click="handleExportXls($t('分销明细'))"
                v-has="'RetailDetailController:export'"
                >{{ $t('导出') }}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>
        <template slot="footer">
          <div style="font-weight: bold">
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('订单金额') }}：</span>
            <span>{{ total.orderPrice.toFixed(2) }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('扣币') }}：</span>
            <span>{{ total.coinValue }}</span>
          </div>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'DistributionDetail',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  data() {
    return {
      moment,
      description: this.$t('分销明细'),
      queryParam: {
        payType: '',
      },
      // 表头
      columns: [
        {
          title: this.$t('业务时间'),
          dataIndex: 'busiTime',
          width: 120,
          align: 'center',
        },
        {
          title: this.$t('业务类型'),
          dataIndex: 'busiType_dictText',
          width: 120,
          align: 'center',
        },
        {
          title: this.$t('会员手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
        },
        {
          title: this.$t('会员卡号'),
          align: 'center',
          dataIndex: 'memberCardNo',
        },
        {
          title: this.$t('会员姓名'),
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: this.$t('支付方式'),
          align: 'center',
          dataIndex: 'payType_dictText',
        },
        {
          title: this.$t('订单金额'),
          align: 'center',
          dataIndex: 'orderPrice',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            return text?parseFloat(text).toFixed(2):0.00.toFixed(2);
          }
        },
        {
          title: this.$t('扣币'),
          align: 'center',
          dataIndex: 'coinValue',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            return text?parseFloat(text).toFixed(2):0.00.toFixed(2);
          }
        },
        {
          title: this.$t('业务员手机号'),
          align: 'center',
          dataIndex: 'salesmanPhone',
        },
        {
          title: this.$t('业务员姓名'),
          align: 'center',
          dataIndex: 'salesmanName',
        },
        {
          title: this.$t('关联订单号'),
          align: 'center',
          dataIndex: 'orderId',
        },
      ],
      url: {
        list: '/retail/retailDetail/list',
        exportXlsUrl: '/retail/retailDetail/exportXls',
      },
      dictOptions: {},
      total: {
        orderPrice: 0,
        coinValue: 0,
      },
      paytypeList: [],
      isCustom: true,
    }
  },
  created() {
    if (this.$route.query.salesmanInfo) {
      this.queryParam.salesmanInfo = this.$route.query.salesmanInfo
      if (this.$route.query.busiTimeBeg && this.$route.query.busiTimeEnd) {
        this.queryParam.busiTimeBeg = this.$route.query.busiTimeBeg
        this.queryParam.busiTimeEnd = this.$route.query.busiTimeEnd
      }
    } else {
      delete this.queryParam.salesmanInfo
      delete this.queryParam.busiTimeBeg
      delete this.queryParam.busiTimeEnd
    }
    this.loadData(1)
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  watch: {
    $route(to, form) {
      if (this.$route.query.salesmanInfo) {
        this.queryParam.salesmanInfo = this.$route.query.salesmanInfo
        if (this.$route.query.busiTimeBeg && this.$route.query.busiTimeEnd) {
          this.queryParam.busiTimeBeg = this.$route.query.busiTimeBeg
          this.queryParam.busiTimeEnd = this.$route.query.busiTimeEnd
        }
      } else {
        delete this.queryParam.salesmanInfo
        delete this.queryParam.busiTimeBeg
        delete this.queryParam.busiTimeEnd
      }
      this.loadData(1)
    },
  },
  methods: {
    initDictConfig() {
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.state = 'Y'
      getAction('/pay/bisPayType/list', param).then((res) => {
        if (res.success) {
          this.paytypeList = res.result.records
          this.paytypeList.unshift({ name: '', nameText: this.$t('全部') })
        } else {
          this.$message.warning(res.message)
        }
      })
      this.getTotal()
    },
    searchQuery() {
      this.loadData(1)
      this.getTotal()
    },
    getTotal() {
      getAction('/retail/retailDetail/total', this.queryParam).then((res) => {
        if (res.success) {
          this.total = res.result
        }
      })
    },
    searchReset() {
      this.queryParam = {
        busiTimeBeg: '',
        busiTimeEnd: '',
        memberInfo: '',
        salesmanInfo: '',
        busiType: undefined,
        payType: '',
      }
      this.loadData(1)
      this.getTotal()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>